package com.zhou.framework.pool;

import com.zhou.util.NumberUtil;
import com.zhou.framework.config.DbSettingConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 勾稽批量校验线程池
 */
@Slf4j
@Configuration
public class ThreadPoolConfig {

    public static final int MAXIMUM_POOL_SIZE = 10;
    public static final int MINIMUM_POOL_SIZE = 3;

    @Bean("scheduledExecutorService")
    public DefaultThreadPool getPool(DbSettingConfig config){
        int poolSize = config.getInteger("thread-pool","poolSize",3);
        int maximumPoolSize = config.getInteger("thread-pool","maximumPoolSize",3);
        boolean debug = config.getBoolean("thread-pool","debug",true);
        maximumPoolSize = NumberUtil.forceBetween(maximumPoolSize,MINIMUM_POOL_SIZE, MAXIMUM_POOL_SIZE);
        poolSize = NumberUtil.forceBetween(poolSize,MINIMUM_POOL_SIZE, maximumPoolSize);

        log.info("线程池大小：{}，最大线程数：{}",poolSize,maximumPoolSize);
        DefaultThreadPool pool = new DefaultThreadPool(poolSize, MAXIMUM_POOL_SIZE);
        pool.setDebug(debug);
        pool.setTrigger(new DefaultThreadTrigger());
        return pool;
    }

}
